﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace CustomMVCCore.Extensions
{
    public static class JsonExtensions
    {
        public static string toJson(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static object fromJson<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
        public static bool IsAjax(this HttpRequest req)
        {
            bool result = false;

            var xreq = req.Headers.ContainsKey("x-requested-with");
            if (xreq)
            {
                result = req.Headers["x-requested-with"] == "XMLHttpRequest";
            }

            return result;
        }
    }
}
